package livesun.justiceeternal.fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import livesun.justiceeternal.MainActivity;
import livesun.justiceeternal.R;
import livesun.justiceeternal.activity.OffLineDownloadActivity;
import livesun.justiceeternal.activity.SearchActivity;
import livesun.justiceeternal.utils.Logger;
import livesun.justiceeternal.utils.ToastUtil;
import livesun.justiceeternal.view.MyCoordinatorLayout;
import livesun.justiceeternal.view.RoundImageView;

/**
 * 类描述：
 * 创建人：livesun
 * 创建时间：2018/1/3.
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */
public class HomePageFragment extends VisibleHintFragment {
    ViewPager mViewPager;
    SlidingTabLayout mSlidingTab;
    Toolbar mToolbar;
    private List<Fragment> fragments=new ArrayList<>();
    private Intent mSearchIntent;
    private Intent mOffLineintent;
    private AppBarLayout appBarLayout;
    private MyCoordinatorLayout coordinatorLayout;

    @Override
    public int getResourcesId() {
        return R.layout.fragment_home_pager;
    }

    public static HomePageFragment newInstance() {
        return new HomePageFragment();
    }

    @Override
    public void initView() {
        mViewPager=findView(R.id.view_pager);
        mSlidingTab=findView(R.id.sliding_tabs);
        appBarLayout = findView(R.id.appbar);
        mToolbar=findView(R.id.toolbar);
        coordinatorLayout = findView(R.id.coordinator);
        setHasOptionsMenu(true);
        initToolBar();
        initViewPager();
        fragments.add(new DescriptionFragment());
        fragments.add(new GetScoreFragment());
        fragments.add(new WebViewFragment());
        fragments.add(new AttentionFragment());
        fragments.add(new FindFragment());
        mSearchIntent = new Intent(getActivity(), SearchActivity.class);
        mOffLineintent = new Intent(getActivity(), OffLineDownloadActivity.class);
    }

    private void initViewPager() {
        HomePagerAdapter mHomeAdapter = new HomePagerAdapter(getChildFragmentManager(), getActivity().getApplicationContext());
        mViewPager.setOffscreenPageLimit(5);
        mViewPager.setAdapter(mHomeAdapter);
        mSlidingTab.setViewPager(mViewPager);
        mViewPager.setCurrentItem(1);
    }


    private void initToolBar() {
        mToolbar.setTitle("");
        ((MainActivity) getActivity()).setSupportActionBar(mToolbar);
    }

    @Override
    public void initListener() {
        findView(R.id.navigation_layout).setOnClickListener(this);
    }

    @Override
    public void onClick(View v, int id) {
        switch (id){
            case R.id.navigation_layout:
                Activity activity = getActivity();
                if (activity instanceof MainActivity) {
                    ((MainActivity) activity).toggleDrawer();
                }
                break;
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.menu_main, menu);

    }
    boolean canScroll = true;
    public void scrootoTop(){
        if(!canScroll){
            return;
        }
        CoordinatorLayout.Behavior behavior =
                ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
        if (behavior instanceof AppBarLayout.Behavior) {
            final AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
            int topAndBottomOffset = appBarLayoutBehavior.getTopAndBottomOffset();
            int i = -mToolbar.getMeasuredHeight();

            ValueAnimator valueAnimator = ValueAnimator.ofInt(topAndBottomOffset, i);
            valueAnimator.setDuration(350);
            valueAnimator.setInterpolator(new AccelerateInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int value = (int) animation.getAnimatedValue();
                    appBarLayoutBehavior.setTopAndBottomOffset(value);
                }
            });
            valueAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    canScroll = true;
                    coordinatorLayout.canScroll(true);
                }

                @Override
                public void onAnimationStart(Animator animation) {
                    super.onAnimationStart(animation);
                    canScroll = false;
                    coordinatorLayout.canScroll(false);
                }
            });
            valueAnimator.start();

//            behavior.onNestedPreScroll(coordinatorLayout,appBarLayout,mViewPager,0,i,new int[]{0,0});
        }
    }

//回收
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        for (Fragment fragment : fragments) {
            if(fragment!=null){
                fragment = null;
            }
        }
        fragments.clear();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(R.id.id_action_search==id){
            //搜索
            startActivity(mSearchIntent);

        }else if(R.id.id_action_download==id){
            //离线缓存
            startActivity(mOffLineintent);
        }

        return super.onOptionsItemSelected(item);
    }

    public boolean canGoBack() {
        boolean canBack=false;

        int currentItem = mViewPager.getCurrentItem();
            Fragment fragment = fragments.get(currentItem);
            if(fragment instanceof WebViewFragment){
                if(((WebViewFragment) fragment).goBack()){
                    canBack=true;
                }else{
                    canBack=false;

                }

            }else if(fragment instanceof AttentionFragment){
                if(((AttentionFragment) fragment).goBack()){
                    canBack=true;
                }else{
                    canBack=false;
                }
            }

        return canBack;
    }

    public boolean goHome(){
        if(mViewPager.getCurrentItem()!=1){
            mViewPager.setCurrentItem(1);
            return true;
        }
       return false;
    }

    public class HomePagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES;


        public HomePagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            TITLES = context.getResources().getStringArray(R.array.sections);

        }


        @Override
        public Fragment getItem(int position) {

            return fragments.get(position);
        }


        @Override
        public int getCount() {
            return TITLES.length;
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }
    }
}
